﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BUS;
using DTO;
namespace QUANLYDIEMHOCSINH
{
    public partial class DANGNHAP : Form
    {
        public DANGNHAP()
        {
            InitializeComponent();
            set_AN();
            DocFile();    
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void DocFile()
        {
            if (File.Exists("taikhoan.txt"))
            {
                FileStream fs = new FileStream("taikhoan.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                while ((s = sr.ReadLine()) != null)
                {
                    txtTaiKhoan.Text = s;
                }
                if (s != null)
                {
                    ckNhoTaiKhoan.Checked = false;
                }
                else
                {
                    ckNhoTaiKhoan.Checked = true;
                }
                fs.Close();
            }
        }
        void GhiFile()
        {
            FileStream fs = new FileStream("taikhoan.txt",FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(txtTaiKhoan.Text);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ckNhoTaiKhoan.Checked == true)
            {
                GhiFile();
            }
            TAIKHOANBUS tkbus = new TAIKHOANBUS();
            TAIKHOANDTO tk = new TAIKHOANDTO();
            tk.TENTK = txtTaiKhoan.Text;
            tk.MATKHAU = txtPass.Text;
            if (tkbus.DangNhap(tk) != 0)
            {
                this.Close();
            }
            else
            {
                MessageBox.Show("Tài Khoản Hoặc Mật Khẩu Không Đúng!");
            }
        }
    }
}
